package mysql

import (
	"comm-sys/userrpc/userrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	GoodsName  string  `gorm:"type:varchar(30);not null"`
	GoodsPrice float32 `gorm:"type:decimal(10,2);not null"`
	GoodsCode  string  `gorm:"type:varchar(30);not null"` //货号
	People     string  `gorm:"type:varchar(30);not null"` //供货商
	UserId     int64   `gorm:"type:int(11);not null"`     //用户编号
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func GoodsList(db *gorm.DB) []*userrpc.GoodsList {
	var goods []*userrpc.GoodsList
	/*
		SELECT
		  users.mobile AS Mobile,
		  goods.goods_name AS GoodsName,
		  goods.goods_price AS GoodsPrice,
		  goods.goods_code AS GoodsCode,
		  goods.people AS People
		FROM
			goods
			LEFT JOIN users ON users.id = goods.user_id
	*/
	db.Debug().Model(&Goods{}).
		Select(
			"users.mobile AS Mobile",
			"goods.goods_name AS GoodsName",
			"goods.goods_price AS GoodsPrice",
			"goods.goods_code AS GoodsCode",
			"goods.people AS People",
		).
		Joins("LEFT JOIN users ON users.id = goods.user_id").
		Find(&goods)
	return goods
}
